<?php
namespace app\know\controller;

use app\know\model\SysConfigModel;
use app\know\controller\AdminKnowBaseController;
use app\know\validate\SysConfigValidate;

/**
 * Class AdminSysConfigController 系统配置管理控制器
 * @package app\know\controller
 */
class AdminSysConfigController extends AdminKnowBaseController
{
    /**
     * 系统配置管理
     * @adminMenu(
     *     'name'   => '系统配置',
     *     'parent' => 'know/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 30000,
     *     'icon'   => '',
     *     'remark' => '系统配置',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $content = hook_one('know_admin_sys_config_index_view');

        if (!empty($content)) 
		{
            return $content;
        }
		$type = $this->request->param('type', 'SUBJECT');
		$this->assign('type', $type);
		
        return $this->fetch();
    }

    public function list()
    {
        $type = $this->request->param('type', 'SUBJECT');
        $pageNumber = $this->request->param('pageNumber', 1, 'intval');
        $pageSize = $this->request->param('pageSize', 10, 'intval');

        $where[] = ['type', 'in', preg_split("/[\s;,]/", $type)];
        $where[] = ['is_delete', '=', 0];
        
        $count = SysConfigModel::where($where)->count();

        $rows = SysConfigModel::where($where)
                                    ->order('order_number', 'desc')
                                    ->page($pageNumber, $pageSize)
                                    ->select();
        
        $this->success('', null, ['rows' => $rows, 'total' => $count]);
    }
    
	public function add()
    {
        $param = $this->request->param();

        $user = $this->getAdminUser();
        $param['create_user'] = $user['user_login'];
        
        try 
		{
			$model = new SysConfigModel();
			$param['uuid'] = $model->getUUID();
            $this->validateFailError($param, 'SysConfig.add');
        } 
		catch (ValidateException $e)
		{
            $this->error($e->getError());
        }

        $row = SysConfigModel::create($param);
        if ($row) 
		{
            $this->opLog([
                'table_name' => 'know_sysconfig',
                'sql' => SysConfigModel::getLastSql(),
                'create_user' => $user['user_login'],
            ]);
            $this->success('添加成功');
        }
        $this->error('添加失败');  
    }
	
	public function delete()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try 
		{
            $this->validateFailError($param, 'SysConfig.delete');
        } 
		catch (ValidateException $e) 
		{
            $this->error($e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try 
		{
            SysConfigModel::where('id', 'in', preg_split("/[\s;,]/", $id))->save(['is_delete' => 1]);
            $this->opLog([
                'table_name' => 'know_sysconfig',
                'sql' => SysConfigModel::getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($id),
            ]);
        } 
		catch (\Throwable $th) 
		{
            $this->error($th->getMessage());
        }
        $this->success('已删除');
    }
	
	public function edit()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try 
		{
            $this->validateFailError($param, 'SysConfig.edit');
        } 
		catch (ValidateException $e) 
		{
            $this->result('', 0, $e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try 
		{
            $d = SysConfigModel::find($id);
            SysConfigModel::where('id', $id)->save($param);
            $this->opLog([
                'table_name' => 'know_sysconfig',
                'sql' => SysConfigModel::getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($d),
            ]);
        } 
		catch (\Throwable $th) 
		{
            $this->error($th->getMessage());
        }
        $this->success('修改成功');
    }

    public function maxOrder()
    {
        $order = SysConfigModel::max('order_number');
        $this->success('', '', $order);
    }
}
